﻿using SmartHRMS.Presentation.Account;
using System;

namespace SmartHRMS.UI.Web.Modules.Administration.Account
{
    public partial class UserDetail1 : System.Web.UI.UserControl, IUserView
    {
        private UserPresenter _Presenter;

        private  string localInsertedBy;

        private  string localUpdatedBy;

        protected void Page_Init(object sender, EventArgs e)
        {
            _Presenter = new UserPresenter(this);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _Presenter.Display();
            }
        }

        #region IUserView Members

        public Guid UserId
        {
            get
            {
                Guid selectedUserId;
                string id = Request.QueryString["id"].ToString();
                if (string.IsNullOrEmpty(id) || id=="0")
                {
                    selectedUserId = Guid.Empty;
                }
                else
                {
                    selectedUserId = new Guid(id);
                }

                return selectedUserId;
            }
        }

        public string UserName
        {
            get
            {
                return txtUserName.Text;
            }
            set
            {
                txtUserName.Text = value;
            }
        }

        public string Password
        {
            get
            {
                return txtPassword.Text;
            }
            set
            {
                txtPassword.Text = value;
            }
        }

        public string RePassword
        {
            get
            {
                return txtRePassword.Text;
            }
            set
            {
                txtRePassword.Text = value;
            }
        }

        public string EmployeeCode
        {
            get
            {
                return txtEmployeeCode.Text;
            }
            set
            {
                txtEmployeeCode.Text = value;
            }
        }

        public string InsertedBy
        {
            get
            {
                return localInsertedBy;
            }
            set
            {
                localInsertedBy = value;
                HyperLinkCreatedBy.NavigateUrl = "UserDetailPage.aspx?id=" + value;
            }
        }

        public string InsertedDate
        {
            get
            {
                return DateTime.Now.ToString();
            }
            set
            {
                txtCreatedDate.Text = value;
            }
        }

        public string UpdatedBy
        {
            get
            {
                return localUpdatedBy;
            }
            set
            {
                localUpdatedBy = value;
                HyperLinkUpdated.NavigateUrl = "UserDetailPage.aspx?id=" + value; 
            }
        }

        public string UpdatedDate
        {
            get
            {
                return DateTime.Now.ToString();
            }
            set
            {
                txtUpdatedDate.Text = value;
            }
        }

        public string InsertedByName
        {
            get
            {
                return HyperLinkCreatedBy.Text;
            }
            set
            {
                HyperLinkCreatedBy.Text = value;
            }
        }

        public string UpdatedByName
        {
            get
            {
                return HyperLinkUpdated.Text;
            }
            set
            {
                HyperLinkUpdated.Text = value;
            }
        }

        public string UserAvailableMessage
        {
            get
            {
                return UserAvailability.InnerText;
            }
            set
            {
                UserAvailability.InnerText = value;
            }
        }

        public string SaveButtonText
        {
            set
            {
                this.btnSaveUser.Text = value;
            }
        }

        #endregion

        #region IBaseView Members

        public void ErrorMessage(string Message)
        {
            if (String.IsNullOrEmpty(Message))
            {
                pnlError.Visible = false;
            }
            else
            {
                pnlError.Visible = true;
            }
            this.litErrorMessage.Text = Message + "<br/>";
        }

        public void InfoMessage(string Message)
        {
            if (String.IsNullOrEmpty(Message))
            {
                pnlInfo.Visible = false;
            }
            else
            {
                pnlInfo.Visible = true;
            }
            this.litInfoMessage.Text = Message + "<br/>";
        }

        public void WarringMessage(string Message)
        {
            if (String.IsNullOrEmpty(Message))
            {
                pnlWarr.Visible = false;
            }
            else
            {
                pnlWarr.Visible = true;
            }
            this.litWarrMessage.Text = Message + "<br/>";
        }

        public void ViewTitle(string title)
        {
            //throw new NotImplementedException();
        }

        #endregion

        protected void btnSaveUser_Click(object sender, EventArgs e)
        {
            _Presenter.SaveUser();
        }

        protected void btnCancelEditUser_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserListPage.aspx");
        }

        protected void txtUserName_TextChanged(object sender, EventArgs e)
        {
            if (_Presenter.CheckForUserNameAvailabl(txtUserName.Text) == true)
            {
                UserAvailability.InnerText = String.Format("Username {0} available!", txtUserName.Text);
                UserAvailability.Style.Add("color","green");
               // UserAvailability.Style.Add("background-image", "/Content/Images/error.png");
            }
            else
            {
                UserAvailability.InnerText = String.Format("Username {0} taken, sorry.If you are on update mode leave this message.", txtUserName.Text);
                UserAvailability.Style.Add("color", "red");
            }
        }
    }
}